#include<iostream>
#include<queue>
#include<map>
#define ll long long
const int maxn=1 << 24;
using namespace std;
int fa[maxn];
int choice[maxn];
int fa1[maxn];
int choice1[maxn];
int ans[20000];
int jg[12][4];
int ansd=0;
int vis[2][1<<24];
map<int,int>m[2];
struct node{
    int state;
    int gz;
   node(int s):state(s){
        gz=0;
    }
};
queue<pair<node,bool> >q;
int main(){
    int start=0;
    int button;
    for(int i=0;i<12;i++){
        cin>>button;
        start|=(button-1)<<(2*i);
        for(int j=0;j<4;j++){
            cin>>jg[i][j];
            jg[i][j]-=1;
        }
    }
    node st(start);
    node zd(0);
    q.push(pair<node,bool>(st,1));
    q.push(pair<node,bool>(zd,0));
    m[1][st.state]=0;
    m[0][zd.state]=0;
    if(st.state==zd.state){
        cout<<0<<endl;
        return 0;
    }
     while(!q.empty()){
        pair<node,bool>f=q.front();
        q.pop();
        if(vis[f.second^1][f.first.state]){
            ansd=f.first.state;
            cout<<m[f.second^1][f.first.state]+f.first.gz<<endl;
            break;
        }
        for(int i=0;i<12;i++){
          int state1=0;
          if(f.second){
        int dq=(f.first.state>>(2*i))&3;
         int nx=jg[i][dq];
         int nxt=(f.first.state>>(2*nx))&3;
        state1=f.first.state^(dq<<(2*i))^(((dq+1)&3)<<(2*i));
        state1=state1^(nxt<<(2*nx))^(((nxt+1)&3)<<(2*nx));
          }
         else if(!f.second){
         int dq=(f.first.state>>(2*i))&3;
         int nx=jg[i][(dq+3)&3];
         int nxt=(f.first.state>>(2*nx))&3;
         state1=f.first.state^(dq<<(2*i))^(((dq+3)&3)<<(2*i));
         state1=state1^(nxt<<(2*nx))^(((nxt+3)&3)<<(2*nx));//变换的是改变前的坐标，而不是改变后
          }
        if(!vis[f.second][state1]){
                node nn(state1);
                vis[f.second][state1]=1;
                nn.gz=f.first.gz+1;
                m[f.second][state1]=nn.gz;
               if(f.second){
                fa[state1]=f.first.state;
                choice[state1]=i+1;
               }
                if(!f.second){
                fa1[state1]=f.first.state;
                choice1[state1]=i+1;
               }
               q.push(pair<node,bool>(nn,f.second));
        }
        }
     }
     int ss=0;
     int cnt=0;
     int ans[30];
     int js=0;
     int stt=ansd;
    //  cout<<choice[ansd]<<" ";
    //  cout<<fa[fa[fa[ansd]]]<<" ";
    //  cout<<start;
    while(start!=stt){
        ans[++cnt]=choice[stt];
        stt=fa[stt];
    }
    for(int i=cnt;i;i--){
        cout<<ans[i]<<" ";
    }
    stt=ansd;
    cnt=0;
    ss=0;
     while(ss!=stt){
        cout<<choice1[stt]<<" ";
        stt=fa1[stt];
     }
    return 0;
}